using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.UI.QueryManager
{
    public partial class InitColumnsForm : Form
    {

        private const string SYSTEM_ID = "System.Id";
        private const string SYSTEM_TITLE = "System.Title";
        private const string SYSTEM_ASSIGNED_TO = "System.AssignedTo";
        private const string SYSTEM_STATE = "System.State";

        private Dictionary<string, FieldDefinition> m_fields;
        private string m_select;

        private InitColumnsForm()
        {
            InitializeComponent();
        }

        public InitColumnsForm(Dictionary<string,FieldDefinition> fields)
            : this()
        {
            this.m_fields = fields;
        }

        public string SelectString
        {
            get { return this.m_select; }
            set { this.m_select = value; }
        }

        private bool m_bOk;

        public bool OK
        {
            get { return this.m_bOk; }
            private set { this.m_bOk = value; }
        }

        private void OnOk(object sender, EventArgs e)
        {
            if (this.m_lbSelectedColumns.Items.Count == 0)
            {
                MessageBox.Show(this, Resources.STRING_SELECT_MORE_COLUMNS, Resources.UI_QUERY_MANAGER_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
            }

            SelectString += ConstantKeyWords.SELECT + " ";
            for (int i = 0; i < this.m_lbSelectedColumns.Items.Count; i++)
            {
                if (i == this.m_lbSelectedColumns.Items.Count - 1)
                    SelectString += "[" + (this.m_lbSelectedColumns.Items[i] as FieldDefinition).ReferenceName + "] ";
                else
                    SelectString += "[" + (this.m_lbSelectedColumns.Items[i] as FieldDefinition).ReferenceName + "], ";
            }
            this.OK = true;
            this.Close();    
        }

        private void OnLoad(object sender, EventArgs e)
        {
            this.m_lbAllColumns.Sorted = true;
            this.m_lbAllColumns.DisplayMember = "Name";
            this.m_lbSelectedColumns.DisplayMember = "Name";
            this.m_lbSelectedColumns.Sorted = false;
            foreach (KeyValuePair<string, FieldDefinition> keyVal in this.m_fields)
                this.m_lbAllColumns.Items.Add(keyVal.Value);

            List<FieldDefinition> itemsToRemove = new List<FieldDefinition>();
            List<FieldDefinition> itemsToAdd = new List<FieldDefinition>();

            foreach (object o in this.m_lbAllColumns.Items)
            {
                switch ((o as FieldDefinition).ReferenceName)
                { 
                    case SYSTEM_ID:
                        itemsToRemove.Add(o as FieldDefinition);
                        itemsToAdd.Add((o as FieldDefinition));
                        break;
                    case SYSTEM_TITLE:
                        itemsToRemove.Add((o as FieldDefinition));
                        itemsToAdd.Add((o as FieldDefinition));
                        break;
                    case SYSTEM_ASSIGNED_TO:
                        itemsToRemove.Add(o as FieldDefinition);
                        itemsToAdd.Add(o as FieldDefinition);
                        break;
                    case SYSTEM_STATE:
                        itemsToRemove.Add(o as FieldDefinition);
                        itemsToAdd.Add(o as FieldDefinition);
                        break;
                    default: break;
                }
            }

            InitColumnOrder(itemsToAdd);


            foreach(FieldDefinition field in itemsToAdd)
            {
                if (this.m_lbSelectedColumns.Items.Contains(field))
                    continue;
                this.m_lbSelectedColumns.Items.Add(field);
            }

            foreach (FieldDefinition field in itemsToRemove)
                this.m_lbAllColumns.Items.Remove(field);
            this.m_lbAllColumns.Focus();
            this.m_btnRemove.Enabled = false;
            this.m_btnAdd.Enabled = false;
        }

        private void InitColumnOrder(List<FieldDefinition> itemsToAdd)
        {
            foreach (FieldDefinition field in itemsToAdd)
            {
                if (field.ReferenceName == SYSTEM_ID)
                {
                    this.m_lbSelectedColumns.Items.Add(field);
                    break;
                }
            }

            foreach (FieldDefinition field in itemsToAdd)
            {
                if (field.ReferenceName == SYSTEM_TITLE)
                {
                    this.m_lbSelectedColumns.Items.Add(field);
                    break;
                }
            }

            foreach (FieldDefinition field in itemsToAdd)
            {
                if (field.ReferenceName == SYSTEM_ASSIGNED_TO)
                {
                    this.m_lbSelectedColumns.Items.Add(field);
                    break;
                }
            }
        }

        private void OnAddClick(object sender, EventArgs e)
        {
            FieldDefinition field = m_lbAllColumns.SelectedItem as FieldDefinition;
            this.m_lbSelectedColumns.Items.Add(field);
            this.m_lbAllColumns.Items.Remove(field);
            this.m_btnAdd.Enabled = false;
        }

        private void OnRemoveClick(object sender, EventArgs e)
        {
            FieldDefinition field = this.m_lbSelectedColumns.SelectedItem as FieldDefinition;
            this.m_lbAllColumns.Items.Add(field);
            this.m_lbSelectedColumns.Items.Remove(field);
            this.m_btnRemove.Enabled = false;
        }

        private void OnAllColumnsSelectionChanged(object sender, EventArgs e)
        {
            this.m_btnAdd.Enabled = true;
            this.m_btnRemove.Enabled = false;
        }

        private void OnSelectedColumnsSelectionChanged(object sender, EventArgs e)
        {
            this.m_btnRemove.Enabled = true;
            this.m_btnAdd.Enabled = false;
        }
    }
}